<?php
/**
 *
 * Author:苏光盼
 * Date:2020/5/21 下午4:23
 * Class:Set.php
 * Web:https://zoodt.com
 * Project:在线工具
 *
 *
 *
 */

namespace App\HttpController\Admin;
use App\Logic\XunSearch;
use App\Model\FLink;
use App\Model\SysCount;
use App\Model\TaskCrontabLog;
use App\Util\Common;
use App\Model\Config as SetModel;
use App\Util\SystemErr;
use EasySwoole\EasySwoole\Command\Utility;
use EasySwoole\EasySwoole\Config;
use EasySwoole\EasySwoole\ServerManager;
use EasySwoole\EasySwoole\Task\TaskManager;
use EasySwoole\Http\Exception\Exception;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\DbManager;
use EasySwoole\Template\Render;
use EasySwoole\Utility\File;

class Set extends Base
{
    public function index(){
        $data = [];
        foreach ( SetModel::create()->order('data_type')->all() as $v ) {
            $data[] = $v->toArray();
        }
        $this->success($data);
    }

    public function update(){
        $data = $this->request()->getParsedBody('data');
        if(!$data) return $this->argError();
        $task = TaskManager::getInstance();
        $task->async( function () use ($data) {
            foreach($data as $k => $v) {
               if($v == 'on'){
                   $v = 'y';
               }
                SetModel::create()->where('name',$k)->update(['value' => $v]);
            }
        });

        $this->success();
    }



    public function set_switch(){
        $data = $this->request()->getParsedBody('data');
        if( isset($data['name']) && $data['name'] && isset($data['value'])) {
            $setData = SetModel::create()->where('name',$data['name'])->get();
            if(!$setData) return $this->argError();
            $setData->value = $data['value'];
            $setData->update();
            return $this->success();
        }
        $this->argError();
    }





    /**
     * 热重启服务 重启所有的Work/task服务
     */
    public function reload(){
        $ser = ServerManager::getInstance()->getSwooleServer()->reload();
        if($ser){
            Utility::opCacheClear();
            $this->success();
        } else {
            $this->argError();
        }
    }



    /**
     * 获取配置缓存时间
     */
    public function cache_time(){
        $this->success(['data' => SET_CACHE_TIME]);
    }



    /**
     * 运行sql语句
     */
    public function sql(){
        $t = $this->request()->getMethod();
        $dbMgr = DbManager::getInstance();
        $Q = new QueryBuilder();
        if($t != 'POST'){
            $Q->raw('show tables');
            $ret = $dbMgr->query($Q,true);
            $ret = $ret->getResult();
            $rows = count($ret);
           $this->lay($ret,$rows);
        } else {
            $data = $this->request()->getParsedBody();
            if(!$data) throw new \Exception('the arg is invalid', 500);
            if($data['t'] == 'tables'){
                if(!isset($data['table']) || !$data['table']) return $this->argError();
                $sql = "desc {$data['table']}";
            } else {
                $sql = Common::make_key_tosbc(trim( trim($data['t'],';'),''));
            }
            $Q->raw($sql);
            try{
                $ret = $dbMgr->query($Q,true)->getResult();
                $this->success($ret);
            } catch ( \Throwable $e){
                $this->argError($e->getMessage());
            }

        }
    }


}